// pages/math/math.js
var config = require('../../config')
// const recordService = require('../../utils/recordService.js')
const recordService = require('../../services/recordService.js')
const sharedService = require('../../services/sharedService.js')

const basicConfig = config.basic
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(options){
    return sharedService.onShareAppMessage(options, function() {
      recordService.get()
    });
  },
  jumpBtn: function(e){
    const that = this
    switch (parseInt(e.target.id)){
      case 0:
        that.deductGold(basicConfig.needGold, function() {
          wx.navigateTo({
            url: './first/first'
          });
        })
        break;
      case 1:
        that.deductGold(basicConfig.needGold, function () {
          wx.navigateTo({
            url: './second/second'
          });
        })
        break;
      case 2:
        that.deductGold(basicConfig.needGold, function () {
          wx.navigateTo({
            url: './third/third'
          });
        })
        break;
      case 3:
        that.deductGold(basicConfig.needGold, function () {
          wx.navigateTo({
            url: './fourth/fourth'
          });
        })
        break;
    }
  },
  deductGold: function(needGold, callback) {
    const globalData = getApp().globalData
    const userInfo = globalData.userInfo
    const record = globalData.record
    const userGold = record.gold ? record.gold : 0
    if (userGold >= needGold) {
      record.gold = record.gold - needGold
      recordService.update(record, function(res) {
        callback()
        globalData.record = record
      })
    } else {
      util.showModel('你没有更多的金币')
    }
  }
})
